#%%
import pickle
import pandas as pd
import numpy as np
import sys

clippredpkl_newiter = sys.argv[1]
# clippredpkl_newiter = '/DATA/taoxianming/rat/data/Mix_analysis/SexAgeDay55andzzcWTinAUT_MMFF/result32/olc-iter1-2024-05-23/output/semisupervise/olc-iter1-2024-05-23_deout_seq_pca.clippredpkl'
clippreddata_newiter = pickle.load(open(clippredpkl_newiter, 'rb'))
df_clipNames_newiter = clippreddata_newiter['df_clipNames']


def get_ind_pvalue_mirror():
    # start from 0
    nK = 34
    nK_mutual = 8
    nK_mirrorhalf = (nK-nK_mutual)//2
    ind_pvalue_mirror = np.concatenate([np.arange(nK_mutual),
                                        np.arange(nK_mirrorhalf) + nK_mirrorhalf + nK_mutual,
                                        np.arange(nK_mirrorhalf) + nK_mutual])
    assert set(ind_pvalue_mirror) == set(np.arange(nK))
    
    def fun_label_mirror(x):
        return  ind_pvalue_mirror[np.array(x)]
    return ind_pvalue_mirror, fun_label_mirror

ind_pvalue_mirror, fun_label_mirror = get_ind_pvalue_mirror()

df_sort = df_clipNames_newiter.sort_values(by=['vnake', 'startFrame', 'isBlack'])
ind_sort = df_sort.index.values
new_label_sort = clippreddata_newiter['cluster_labels'][ind_sort] - 1 #start from 0
new_label_W = new_label_sort[::2]
new_label_B = new_label_sort[1::2]
new_label_B_mirror = fun_label_mirror(new_label_B)
print('mean mirror', np.mean(new_label_W == new_label_B_mirror))

#%%
if True:
    clippredpkl_old_perc66 = '/home/liying_lab/chenxf/ml-project/论文图表/semisupervised分类/MM_FF_Representive_K34.clippredpkl'
    clippreddata_old_perc66 = pickle.load(open(clippredpkl_old_perc66, 'rb'))
    df_clipNames_old_perc66 = clippreddata_old_perc66['df_clipNames']
    ind_rawclip = clippreddata_old_perc66['ind_rawclip']
    df_newiter_sub = df_clipNames_newiter.loc[ind_rawclip]
    assert np.all(df_clipNames_old_perc66['startFrame'].values == df_newiter_sub['startFrame'].values)
    print('acc class', np.mean(clippreddata_newiter['cluster_labels'][ind_rawclip] == clippreddata_old_perc66['cluster_labels']))